package com.example.demo.utils;

import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Locale;

/**
 * Description:
 *
 * @author : laughing
 * DateTime: 2021-06-25 17:04
 */
@Component
public class MessageUtils {

    @Resource
    private MessageSource messageSource;

    private static MessageSource staticMessageSource;

    @PostConstruct
    public void init() {
        MessageUtils.staticMessageSource = messageSource;
    }

    /**
     * 获取消息
     * @param messageKey
     * @return
     */
    public static String getMessage(String messageKey) {
        Locale locale = LocaleContextHolder.getLocale();
        return staticMessageSource.getMessage(messageKey, null, locale);
    }
}
